package com.zy.pro.system.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.model.City;
import com.zy.pro.city.service.CityService;
import com.zy.pro.city.web.form.CitySearchForm;
import com.zy.pro.contact.model.Contact;
import com.zy.pro.contact.service.ContactService;
import com.zy.pro.contact.web.form.ContactSearchForm;
import com.zy.pro.notice.model.Notice;
import com.zy.pro.notice.service.NoticeService;
import com.zy.pro.notice.web.form.NoticeSearchForm;
import com.zy.pro.order.model.Comment;
import com.zy.pro.order.service.CommentService;
import com.zy.pro.order.web.form.CommentSearchForm;
import com.zy.pro.scroll.model.ScrollImage;
import com.zy.pro.scroll.service.ScrollImageService;
import com.zy.pro.scroll.web.form.ScrollImageSearchForm;
import com.zy.pro.shop.model.Cases;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.service.CasesService;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.shop.web.form.CasesSearchForm;
import com.zy.pro.shop.web.form.ShopSearchForm;
import com.zy.pro.system.model.Employee;
import com.zy.pro.system.web.form.MenuForm;
import com.zy.pro.system.web.form.SearchForm;
import com.zy.pro.user.model.User;
import com.zy.pro.user.service.UserService;
import com.zy.pro.user.web.form.UserSearchForm;
import com.zy.utils.MailInfo;
import com.zy.utils.SessionUtils;
import com.zy.utils.SimpleMailSender;

/**
 * 布局控制器
 *
 */
@Controller
@RequestMapping("/control/layoutController")
public class LayoutController extends BaseController {

	private static final Logger logger = Logger.getLogger(LayoutController.class);
	
	/**
	 * 首页滚动图管理服务类
	 */
	@Resource(name = "scrollImageServiceImpl")
	private ScrollImageService scrollImageService;
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 案例管理服务类
	 */
	@Resource(name = "casesServiceImpl")
	private CasesService casesService;
	
	/**
	 * 城市管理服务类
	 */
	@Resource(name = "cityServiceImpl")
	private CityService cityService;
	
	/**
	 * 公告管理服务类
	 */
	@Resource(name = "noticeServiceImpl")
	private NoticeService noticeService;
	
	/**
	 * 评论管理服务类
	 */
	@Resource(name = "commentServiceImpl")
	private CommentService commentService;
	
	/**
	 * 联系我们管理服务类
	 */
	@Resource(name = "contactServiceImpl")
	private ContactService contactService;
	
	/**
	 * 系统管理员管理服务类
	 */
	@Resource(name = "userServiceImpl")
	private UserService userService;
	
	/**
	 * 显示引入页面
	 * @param request
	 * @param map
	 * @return
	 */
	/*@RequestMapping("/displaySysLogin")
	public String displaySysLogin(HttpServletRequest request, ModelMap map){
		try {
			
			return "control/system/login";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}*/
	
	
	/**
	 * 显示header
	 * @throws Exception 
	 */
	/*@RequestMapping("/displayHeader")
	public String displayHeader(HttpServletRequest request,ModelMap map){
		logger.info("displayHeader for LayoutController start...");
		try {
			Employee employee = SessionUtils.getEmpl(request);
			map.addAttribute("employee", employee);
			
			map.addAttribute("ipAddr",this.getIpAddr(request));
			return "control/layout/header";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}*/
	
	/**
	 * 显示footer
	 */
	/*@RequestMapping("/displayFooter")
	public String displayFooter(HttpServletRequest request, ModelMap map){
		map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		return "control/layout/footer";
	}*/
	
	/**
	 * 显示首页面
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayIndex")
	public String displayIndex(City city,ScrollImageSearchForm scrollForm,ShopSearchForm shopForm,
							CasesSearchForm casesForm,NoticeSearchForm noticeForm,HttpServletRequest request, ModelMap map){
		//map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayIndex.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			//如果city主键为空,则判断是查询ALL设置一下查询表单的城市主键也为空
			if(city.getCityId() == null){
				scrollForm.setCityId(null);
				shopForm.setCityId(null);
				casesForm.setCaseCityId(null);
			}
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			//首页滚动图信息
			scrollForm.getOrderby().put("scrollCreateTime", "desc");
			PageView<ScrollImage> scrollPageView = new PageView<ScrollImage>(5, scrollForm.getPage());
			scrollForm.setFirstResult(scrollPageView.getFirstResult());
			scrollForm.setMaxResult(scrollPageView.getMaxresult());
			scrollPageView.setQueryResult(scrollImageService.getData(scrollForm));
			map.addAttribute("scrollPageView", scrollPageView);
			map.addAttribute("scrollForm", scrollForm);
			
			
			//公告信息
			noticeForm.getOrderby().put("noticeCreateTime", "desc");
			noticeForm.setNoticeStatus(1);//状态(1:正常)
			PageView<Notice> noticePageView = new PageView<Notice>(6, noticeForm.getPage());
			noticeForm.setFirstResult(noticePageView.getFirstResult());
			noticeForm.setMaxResult(noticePageView.getMaxresult());
			noticePageView.setQueryResult(noticeService.getData(noticeForm));
			map.addAttribute("noticePageView", noticePageView);
			//公告记录数
			int noticeCount = 0;
			if(!noticePageView.getRecords().isEmpty()){
				noticeCount = noticePageView.getRecords().size();
			}
			map.addAttribute("noticeForm", noticeForm);
			map.addAttribute("noticeCount", noticeCount);
			
			
			//查询最新店铺信息
			shopForm.getOrderby().put("shopCreateTime", "desc");
			shopForm.setShopStatus(1);//正常使用的店铺(1:正常,2:冻结,3:删除)
			PageView<Shop> shopPageView = new PageView<Shop>(8, shopForm.getPage());
			shopForm.setFirstResult(shopPageView.getFirstResult());
			shopForm.setMaxResult(shopPageView.getMaxresult());
			shopPageView.setQueryResult(shopService.getData(shopForm));
			map.addAttribute("shopPageView", shopPageView);
			map.addAttribute("shopForm", shopForm);
			
			
			//查询最新案例信息
			casesForm.getOrderby().put("caseCreateTime", "desc");
			//设置城市主键
			if(shopForm.getCityId() != null){
				casesForm.setCaseCityId(shopForm.getCityId());
			}
			casesForm.setCasesStatus(1);//案例状态(0:待上架,1:已上架,2:已下架)
			PageView<Cases> casesPageView = new PageView<Cases>(10, casesForm.getPage());
			casesForm.setFirstResult(casesPageView.getFirstResult());
			casesForm.setMaxResult(casesPageView.getMaxresult());
			casesPageView.setQueryResult(casesService.getData(casesForm));
			map.addAttribute("casesPageView", casesPageView);
			map.addAttribute("casesForm", casesForm);
			
			return "control/jsp/index";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 显示店铺列表页面
	 * @param shopForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayShop")
	public String displayShop(City city,SearchForm searchform,ShopSearchForm shopForm,HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_SHOP,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayShop.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			//如果city主键为空,则判断是查询ALL设置一下查询表单的城市主键也为空
			if(city.getCityId() == null){
				shopForm.setCityId(null);
			}
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			
			//查询最新店铺信息
			shopForm.getOrderby().put("shopCreateTime", "desc");
			shopForm.setShopStatus(1);//正常使用的店铺(1:正常,2:冻结,3:删除)
			PageView<Shop> shopPageView = new PageView<Shop>(18, shopForm.getPage());
			shopForm.setFirstResult(shopPageView.getFirstResult());
			shopForm.setMaxResult(shopPageView.getMaxresult());
			shopPageView.setQueryResult(shopService.getData(shopForm));
			map.addAttribute("pageView", shopPageView);
			map.addAttribute("shopForm", shopForm);
			//网站搜索条件
			map.addAttribute("searchform", searchform);
			
			return "control/jsp/shop";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 显示案例列表页面
	 * @param shopForm
	 * @param casesForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayCase")
	public String displayCase(City city,SearchForm searchform,ShopSearchForm shopForm,CasesSearchForm casesForm,HttpServletRequest request, ModelMap map){
		//map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_CASE,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayCase.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			//如果city主键为空,则判断是查询ALL设置一下查询表单的城市主键也为空
			if(city.getCityId() == null){
				shopForm.setCityId(null);
				casesForm.setCaseCityId(null);
			}
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			//查询最新案例信息
			casesForm.getOrderby().put("caseCreateTime", "desc");
			//设置城市主键
			if(shopForm.getCityId() != null){
				casesForm.setCaseCityId(shopForm.getCityId());
			}
			casesForm.setCasesStatus(1);//案例状态(0:待上架,1:已上架,2:已下架)
			PageView<Cases> casesPageView = new PageView<Cases>(20, casesForm.getPage());
			casesForm.setFirstResult(casesPageView.getFirstResult());
			casesForm.setMaxResult(casesPageView.getMaxresult());
			casesPageView.setQueryResult(casesService.getData(casesForm));
			map.addAttribute("pageView", casesPageView);
			map.addAttribute("casesForm", casesForm);

			//网站搜索条件
			map.addAttribute("searchform", searchform);
			
			return "control/jsp/case";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 显示店铺详情页面
	 * @param shopForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayShopDetail")
	public String displayShopDetail(City city,ShopSearchForm shopForm,HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_SHOP,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayShop.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			//查询最新店铺详细信息
			Shop shop = shopService.find(shopForm.getShopId());
			map.addAttribute("shop", shop);
			
			
			//查询店铺相关案例信息(列表)
			CasesSearchForm casesForm = new CasesSearchForm();
			//查询最新案例信息
			casesForm.getOrderby().put("caseCreateTime", "desc");
			//设置店铺主键
			casesForm.setCaseShopId(shopForm.getShopId());
			casesForm.setCasesStatus(1);//案例状态(0:待上架,1:已上架,2:已下架)
			PageView<Cases> casesPageView = new PageView<Cases>(10, casesForm.getPage());
			casesForm.setFirstResult(casesPageView.getFirstResult());
			casesForm.setMaxResult(casesPageView.getMaxresult());
			casesPageView.setQueryResult(casesService.getData(casesForm));
			map.addAttribute("casesPageView", casesPageView);
			map.addAttribute("casesForm", casesForm);
			
			
			//查询店铺相关评论(列表)
			CommentSearchForm commentForm = new CommentSearchForm();
			//查询最新评论信息
			commentForm.getOrderby().put("commentCreateTime", "desc");
			//设置店铺主键
			commentForm.setCommentShopId(shopForm.getShopId());
			PageView<Comment> commentPageView = new PageView<Comment>(12, commentForm.getPage());
			commentForm.setFirstResult(commentPageView.getFirstResult());
			commentForm.setMaxResult(commentPageView.getMaxresult());
			commentPageView.setQueryResult(commentService.getData(commentForm));
			map.addAttribute("commentPageView", commentPageView);
			map.addAttribute("commentForm", commentForm);
			
			return "control/jsp/shop_del";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 店铺详情页异步加载案例列表
	 * @param casesForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/ajaxShopCaseList")
	public String ajaxShopCaseList(CasesSearchForm casesForm,HttpServletRequest request, ModelMap map){
		//map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		try {
			
			//查询最新案例信息
			casesForm.getOrderby().put("caseCreateTime", "desc");
			//设置案例状态
			casesForm.setCasesStatus(1);//案例状态(0:待上架,1:已上架,2:已下架)
			PageView<Cases> casesPageView = new PageView<Cases>(10, casesForm.getPage());
			casesForm.setFirstResult(casesPageView.getFirstResult());
			casesForm.setMaxResult(casesPageView.getMaxresult());
			casesPageView.setQueryResult(casesService.getData(casesForm));
			map.addAttribute("casesPageView", casesPageView);
			map.addAttribute("casesForm", casesForm);

			
			return "control/jsp/ajaxShopCase";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 店铺详情页异步加载店铺评论信息列表
	 * @param casesForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/ajaxShopCommentList")
	public String ajaxShopCommentList(CommentSearchForm commentForm,HttpServletRequest request, ModelMap map){
		//map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		try {
			
			//查询最新评论信息
			commentForm.getOrderby().put("commentCreateTime", "desc");
			
			PageView<Comment> commentPageView = new PageView<Comment>(12, commentForm.getPage());
			commentForm.setFirstResult(commentPageView.getFirstResult());
			commentForm.setMaxResult(commentPageView.getMaxresult());
			commentPageView.setQueryResult(commentService.getData(commentForm));
			map.addAttribute("commentPageView", commentPageView);
			map.addAttribute("commentForm", commentForm);
			
			
			return "control/jsp/ajaxShopComment";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	
	
	/**
	 * 显示案例列表页面
	 * @param shopForm
	 * @param casesForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayCaseDetail")
	public String displayCaseDetail(City city,CasesSearchForm casesForm,HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_CASE,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayCase.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			//查询案例的基本信息(包括案例图片列表)
			Cases cases = casesService.find(casesForm.getCaseId());
			map.addAttribute("cases", cases);
			
			//查询案例所属于的店铺的基本信息
			Shop shop = shopService.find(cases.getCaseShopId());
			map.addAttribute("shop", shop);
			
			//查询案例的评论信息列表
			CommentSearchForm commentForm = new CommentSearchForm();
			//查询最新评论信息
			commentForm.getOrderby().put("commentCreateTime", "desc");
			//设置案例主键
			commentForm.setCommentCaseId(casesForm.getCaseId());
			PageView<Comment> commentPageView = new PageView<Comment>(12, commentForm.getPage());
			commentForm.setFirstResult(commentPageView.getFirstResult());
			commentForm.setMaxResult(commentPageView.getMaxresult());
			commentPageView.setQueryResult(commentService.getData(commentForm));
			map.addAttribute("commentPageView", commentPageView);
			map.addAttribute("commentForm", commentForm);
			
			return "control/jsp/case_del";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 案例详情页异步加载案例评论信息列表
	 * @param casesForm
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/ajaxCaseCommentList")
	public String ajaxCaseCommentList(CommentSearchForm commentForm,HttpServletRequest request, ModelMap map){
		//map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		try {
			
			//查询最新评论信息
			commentForm.getOrderby().put("commentCreateTime", "desc");
			
			PageView<Comment> commentPageView = new PageView<Comment>(12, commentForm.getPage());
			commentForm.setFirstResult(commentPageView.getFirstResult());
			commentForm.setMaxResult(commentPageView.getMaxresult());
			commentPageView.setQueryResult(commentService.getData(commentForm));
			map.addAttribute("commentPageView", commentPageView);
			map.addAttribute("commentForm", commentForm);

			
			return "control/jsp/ajaxCaseComment";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 展示用户登录页面
	 * @param city
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayUserLogin")
	public String displayUserLogin(City city,HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayUserLogin.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			return "control/jsp/login/userLogin";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 展示商家登录页面
	 * @param city
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayShopLogin")
	public String displayShopLogin(City city,HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayShopLogin.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			return "control/jsp/login/sellerLogin";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 展示用户注册页面
	 * @param city
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayUserRegister")
	public String displayUserRegister(City city,HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayUserRegister.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			return "control/jsp/login/userRegister";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 展示用户注册页面
	 * @param city
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayUserFindPassword")
	public String displayUserFindPassword(City city,String findPasswordStatus, User user, HttpServletRequest request, ModelMap map){
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayUserFindPassword.do");
			//设置选择的城市信息到Session中
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			String anstatus = "";
			//判断找回密码执行流程。
			if(findPasswordStatus == null || "".equals(findPasswordStatus)){
				findPasswordStatus = "1";
			}else if("1".equals(findPasswordStatus) && user != null){
				user = userService.findUserByUserName(user);
				findPasswordStatus = "2";
			}else if("2".equals(findPasswordStatus) && user != null){
				UserSearchForm userSearchForm = new UserSearchForm();
				userSearchForm.setUserName(user.getUserName());
				if(user.getQuestionFirst() != null && !"".equals(user.getQuestionFirst()) && user.getAnswerFirst() != null && !"".equals(user.getAnswerFirst())){
					userSearchForm.setQuestionFirst(user.getQuestionFirst());
					userSearchForm.setAnswerFirst(user.getAnswerFirst());
				}
				if(user.getQuestionSecond() != null && !"".equals(user.getQuestionSecond()) && user.getAnswerSecond() != null && !"".equals(user.getAnswerSecond())){
					userSearchForm.setQuestionSecond(user.getQuestionSecond());
					userSearchForm.setAnswerSecond(user.getAnswerSecond());
				}
				List<User> users = userService.findAll(userSearchForm);
				if(!users.isEmpty()){
					findPasswordStatus = "3";
				}else{
					anstatus = "The answer is not correct, please answers again";
					user = userService.findUserByUserName(user);
				}
			}
			//找回密码的状态(1:第一步验证用户名,2:第二步输入用户密保问题,3:第三步验证邮件发送邮件)
			map.addAttribute("findPasswordStatus", findPasswordStatus);
			map.addAttribute("user", user);
			map.addAttribute("answerStatus", anstatus);
			return "control/jsp/login/userFindPassword";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 网站头部搜索店铺/案例
	 * @param form
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/search")
	public String search(City city,SearchForm form,HttpServletRequest request, ModelMap map){
		try {
			
			if(form.getSearchValue() != null && !"".equals(form.getSearchValue())){
				form.setSearchValue(form.getSearchValue().trim());
			}
			
			//判断是搜索店铺还是案例(1:店铺,2:案例)
			if("1".equals(form.getSearchType())){
				ShopSearchForm shopForm = new ShopSearchForm();
				shopForm.setSearchValue(form.getSearchValue());
				return displayShop(city,form, shopForm, request, map );
			}else if("2".equals(form.getSearchType())){
				CasesSearchForm casesForm = new CasesSearchForm();
				casesForm.setSearchValue(form.getSearchValue());
				return displayCase(city,form, new ShopSearchForm(), casesForm, request, map);
			}
			return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	
	/**
	 * 网站首页 公告点击更多是 展示 公告列表页信息
	 */
	@RequestMapping("/displayNoticeList")
	public String displayNoticeList(City city,NoticeSearchForm form, ModelMap map,HttpServletRequest request) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayNoticeList.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			form.getOrderby().put("noticeCreateTime", "desc");
			//公告的状态(1:正常)
			form.setNoticeStatus(1);
			//创建页面对象
			PageView<Notice> pageView = new PageView<Notice>(5, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(noticeService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("noticeForm", form);
			
			return "control/jsp/notice";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 网站首页 公告点击更多是 展示 公告列表页信息
	 */
	@RequestMapping("/displayNoticeDetail")
	public String displayNoticeDetail(City city,NoticeSearchForm form, ModelMap map,HttpServletRequest request) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayNoticeDetail.do");
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			form.getOrderby().put("noticeCreateTime", "desc");
			//公告的状态(1:正常)
			form.setNoticeStatus(1);
			//创建页面对象
			PageView<Notice> pageView = new PageView<Notice>(1, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(noticeService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("noticeForm", form);
			
			return "control/jsp/notice";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 网站联系我们页面
	 */
	@RequestMapping("/displayContact")
	public String displayContact(City city,Contact contact, ModelMap map,HttpServletRequest request) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_INDEX,MenuForm.PAR_TYPE_INDEX_INDEX,null);
			map.addAttribute("menuForm", menuForm);
			
			//选择的城市信息
			map.addAttribute("reqUrl", "/control/layoutController/displayContact.do");
			/*//设置选择的城市信息到Session中
			//SessionUtils.setCity(request,city);
			if(city != null && city.getCityId() != null && city.getCityName() != null && !"".equals(city.getCityName())){
				map.addAttribute("cityValue", "cityId="+city.getCityId().toString()+"&cityName="+city.getCityName());
			}else{
				map.addAttribute("cityValue", "");
			}*/
			//获取城市信息(判断是否是用户登录的,如果用户已经登录查看用户选择的城市-如果用户切换城市,则替换保存用户选择的城市主键)
			city = getCity(city,request);
			map.addAttribute("city", city);
			map.addAttribute("cityValue", getCityValue(city));
			
			
			//城市信息
			List<City> cityList = getAllCitys(new CitySearchForm());
			map.addAttribute("cityList", cityList);
			
			List<Contact> cList = new ArrayList<Contact>();
			cList = contactService.findAll(new ContactSearchForm());
			if(!cList.isEmpty()){
				contact = cList.get(0);
			}
			
			map.addAttribute("contact", contact);
			return "control/jsp/contact";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	
	
	
	/**
	 * 按城市的创建时间排序,获取所有城市
	 * @param cForm
	 * @return
	 */
	public List<City> getAllCitys(CitySearchForm cForm){
		List<City> cityList = new ArrayList<City>();
		try {
			cForm.getOrderby().put("cityCreateTime", "desc");
			cityList = cityService.findAll(cForm);
			return cityList;
		} catch (Exception e) {
			e.printStackTrace();
			return cityList;
		}
	}
	
	/**
	 * 获取城市信息(如果用户登录,设置城市信息为用户选择的城市,如果用户切换城市,则替换保存用户切换的城市主键)
	 * @param city
	 * @param request
	 * @return
	 */
	public City getCity(City city,HttpServletRequest request){
		try {
			User userSession = SessionUtils.getUser(request);
			if(userSession != null && city != null && city.getCityId() != null && ((userSession.getCityId() != null && city.getCityId() != userSession.getCityId()) || userSession.getCityId() == null)){
				User user = new User();
				user.setUserId(userSession.getUserId());
				user.setCityId(city.getCityId());
				//更新用户城市主键
				userService.updateBySelective(user);
				//查询最新用户信息
				user = userService.find(user.getUserId());
				//删除session中用户信息
				SessionUtils.removeUser(request);
				//重新设置用户信息
				SessionUtils.setUser(request, user);
			}else if(userSession != null && userSession.getCityId() != 0 && (city == null || city.getCityId() == null)){
				city = cityService.find(userSession.getCityId());
			}
			if(city != null && city.getCityId() != null && city.getCityId() == 0){
				city.setCityId(null);
			}
			return city;
		} catch (Exception e) {
			logger.error(e);
			return city;
		}
	}
	
	
	/**
	 * 根据城市信息获取查询条件(拼接成字符串)
	 * 判断是否是用户登录已经后的操作如果是,每次用户切换城市时,将被切换的城市存储到他的信息当中去
	 * @param city
	 * @return
	 */
	public String getCityValue(City city){
		String str = "";
		try {
			if(city != null && city.getCityId() != null && city.getCityName() != null && !"".equals(city.getCityName())){
				str = "cityId="+city.getCityId().toString()+"&cityName="+city.getCityName();
			}
			return str;
		} catch (Exception e) {
			logger.error(e);
			return str;
		}
	}
	
	/**
	 * 退出
	 * @param request
	 * @return
	 */
	@RequestMapping("/loginOut")
	public String loginOut(String roleType,Integer cityId,ModelMap map,HttpServletRequest request){
		if("1".equals(roleType)){
			//删除session中的用户信息
			SessionUtils.removeUser(request);
		}else if("2".equals(roleType)){
			//删除session中的店家信息
			SessionUtils.removeShopUser(request);
		}else if("3".equals(roleType)){
			//删除session中的管理员信息
			SessionUtils.removeEmpl(request);
		}
		//删除角色类型
		SessionUtils.removeRoleType(request);
		
		ShopSearchForm shopSearchForm = new ShopSearchForm();
		if(cityId != null){
			shopSearchForm.setCityId(cityId);
		}
		
		if("3".equals(roleType)){
			//删除session中的管理员信息
			return "/control/system/login";
		}
		return displayIndex(new City(),new ScrollImageSearchForm(),shopSearchForm,new CasesSearchForm(),new NoticeSearchForm(),request,map);
	}
	
	@RequestMapping("/toMail")
	public String toMail(String message,String email,ModelMap map,HttpServletRequest request){
		//根据路径读取内容
		MailInfo mailInfo = MailInfo.getInstance(request);
		if(message == null || "".equals(message)){
			message = "Please click the following link to activate your account<br><a href='"+mailInfo.getProperty("mailPath")+"control/user/userController/activateAccount.do'>"+mailInfo.getProperty("mailPath")+"activateAccount</a>";
		}

		String msg = SimpleMailSender.sendFileMail(email, 
				mailInfo.getProperty("mailName"), 
				mailInfo.getProperty("mailPassword"), 
				//"Please click the following link to activate your account<br><a href='"+mailInfo.getProperty("mailPath")+"control/user/userController/activateAccount.do?userId="+user.getUserId()+"'>"+mailInfo.getProperty("mailPath")+"activateAccount</a>", 
				message,
				"To activate the account");
		map.addAttribute("msg", msg);
		return "/control/jsp/mail";
	}
}
